package workflow

import (
	"cdam/enum"
	"cdam/models"
	"cdam/qiongqi/listener"
	"cdam/util"
	"fmt"
	"strconv"
)

type Mategroupworkflow struct{

}

func (m *Mategroupworkflow) BeforeEndInstance(parameter listener.ListenParameter) listener.ListenParameter {
	fmt.Print("hello world")
	return parameter
}

func (m *Mategroupworkflow) AfterEndInstance(parameter listener.ListenParameter) listener.ListenParameter {
	// 完成节点
	condition := new(models.DefFlowInstanceTask)
	condition.Instanceid = parameter.InstanceId
	condition.Processid = parameter.ProcessId
	condition.Nodecode = "StartEvent_1"
	startFlowProcessNode, _ := models.GetDefFlowInstanceTask(*condition)
	count,result,err := models.GetAllFlowInstanceTaskCandidateByNode(startFlowProcessNode.Taskid,parameter.InstanceId)
	if err == nil && count > 0 {
		for _,data := range result {
			inputdata,e := util.ConvertStringToMap(data.InputData)
			id := util.ConverInterfaceToString(util.Interface2String(inputdata["id"]))
			if i,_ :=strconv.ParseInt(id,10,64); e == nil {
				r,er := models.GetMategroupById(i)
				if er == nil {
					r.Status = enum.FINISHED.String()
					models.UpdateMategroupById(r)
				}
			}

		}
	}
	//}
	return parameter
}

func (m *Mategroupworkflow) BeforeNextStep(parameter listener.ListenParameter) listener.ListenParameter {
	if parameter.BtnClient.String() == enum.BTN_CLIENT_TYPE_AGREE.String() && parameter.CurrentNodeCode == "GroupApprove" {
		p := util.ConverInterfaceToString(parameter.CurrentNodeParmeter)
		mapp, _ := util.ConvertStringToMap(p)
		if mapp == nil {
			mapp = map[string]interface{}{}
		}
		mapp["gapprove"] = int64(1)
		parameter.CurrentNodeParmeter = mapp
	}
	if parameter.BtnClient.String() == enum.BTN_CLIENT_TYPE_AGREE.String() && parameter.CurrentNodeCode == "ManagerApprove" {
		p := util.ConverInterfaceToString(parameter.CurrentNodeParmeter)
		mapp, _ := util.ConvertStringToMap(p)
		if mapp == nil {
			mapp = map[string]interface{}{}
		}
		mapp["mapprove"] = int64(1)
		parameter.CurrentNodeParmeter = mapp
	}
	return parameter
}

func (m *Mategroupworkflow) AfterNextStep(parameter listener.ListenParameter) listener.ListenParameter {
	return parameter
}

func (m *Mategroupworkflow) BeforInstanceStart() {
	fmt.Print("hello world")
}

func (m *Mategroupworkflow) AfterInstanceStart() {
	fmt.Print("hello world")
}

var _ listener.Listener = new(Mategroupworkflow)
